package com.cqc.viewdraghelper01;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by a on 2018/3/23.
 * QQ侧拉栏
 */

public class DragLayout extends FrameLayout {

    private ViewDragHelper dragHelper;
    private int width;//DragLayout的宽
    private int height;//DragLayout的高
    private int range;//拖动范围
    private View leftView;//背景面板
    private View mainView;//主面板

    public DragLayout(@NonNull Context context) {
        this(context, null);
    }

    public DragLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dragHelper = ViewDragHelper.create(this, 1.0f, callback);
    }


    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return true;
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            super.onViewPositionChanged(changedView, left, top, dx, dy);
            //当拖动的是leftView的时候，把它固定住，把leftView的dx传递给mainView
            if (changedView == leftView) {
                leftView.layout(0, 0, width, height);
                int newLeft = mainView.getLeft() + dx;
                newLeft = fixLeft(newLeft);
                mainView.layout(newLeft, 0, width + newLeft, height);
            }

            addAnimation(dx);
            invalidate();
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return range;
        }

        /**
         * 释放view时调用
         * @param releasedChild
         * @param xvel  x轴速度，往右+，往左-
         * @param yvel
         */
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            if (mainView.getLeft() > range / 2) {
                openMainView();
            } else {
                closeMinaView();
            }
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            //限制mainView的拖动范围 [0,range]
            if (child == mainView) {
                left = fixLeft(left);
            }
            return left;
        }
    };


    private void addAnimation(int dx) {
        //注意：分子必须*1.0
        float percent = ((mainView.getLeft() + dx) * 1.0f / range);//[0,1]

        leftView.setScaleX(percent * 0.5f + 0.5f);
        leftView.setScaleY(percent * 0.5f + 0.5f);

        leftView.setTranslationX(-0.5f + percent * 0.5f);
        leftView.setAlpha(0.2f + percent * 0.8f);

        mainView.setScaleX(1.0f - percent * 0.5f / 2);
        mainView.setScaleY(1.0f - percent * 0.5f / 2);
    }

    private void openMainView() {
        //方法一：滑动滑动的比较生硬
//        mainView.layout(range, 0, width + range, height);
        //方法二：平滑的滑动
        if (dragHelper.smoothSlideViewTo(mainView, range, 0)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    private void closeMinaView() {
        //方法一：滑动滑动的比较生硬
//        mainView.layout(0, 0, width, height);
        //方法二：平滑的滑动
        if (dragHelper.smoothSlideViewTo(mainView, 0, 0)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    private int fixLeft(int left) {
        if (left < 0) {
            left = 0;
        } else if (left > range) {
            left = range;
        }
        return left;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        range = (int) (0.6 * width);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        leftView = getChildAt(0);
        mainView = getChildAt(1);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (dragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return dragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        try {
            dragHelper.processTouchEvent(event);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
